from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
# 使用KNN算法进行鸢尾花的分类预测

# 1 获取数据 - 清洗数据（省略）
iris = load_iris()  # iris.data 特征值 iris.target目标值
print(iris.data, iris.target)
# 2 划分数据集
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target)
# 3 特征工程 - 标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
# 测试集的转换只需要调用transform 目的是让训练集和测试集之间产生一定的关联
x_test = transfer.transform(x_test)
# 4 训练模型
estimator = KNeighborsClassifier(n_neighbors=1)
estimator.fit(x_train, y_train)
# 5 模型评估
y_predict = estimator.predict(x_test)
# 预测值与真实值之间对比
print(y_predict == y_test)
# 直接计算准确率
print(estimator.score(x_test, y_test))